package com.risen.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.risen.bean.Book;
import com.risen.form.OrderItemForm;
import com.risen.service.BookService;
import com.risen.service.OrderService;
import com.risen.util.Paging;

/**
 * @author sen
 *	图书模块控制层
 */

@Controller
@RequestMapping("book")
public class BookController {
	
	@Resource
	private BookService bookService;
	@Resource
	private OrderService orderService;
	
	/**
	 * @return
	 *  按照分类获取图书列表(分页)
	 */
	@RequestMapping(value = "list")
	public String queryBookList(HttpServletRequest request){
		//查询条件
		String term=request.getParameter("term");
		/*
		 * 1.获取查询条件参数：查询条件、查询页码（如果页码参数为空，则设置为1）
		 */
		String currentPage=request.getParameter("currentPage");
		String condionRad=request.getParameter("condionRad");
		if(currentPage==null){
			currentPage="1";
		}
		Book book=new Book();
		if (term != null && !term.isEmpty()) {
			//按书分类查询
			String cid=request.getParameter("cid");
			if(cid!=null&&!cid.trim().isEmpty()){
				book.setCid(Integer.parseInt(cid));
			}
			//按书名模糊查询
			if("3".equalsIgnoreCase(condionRad)){
				book.setBname(term);
			}
			//按作者查询
			if("2".equalsIgnoreCase(condionRad)){
				book.setAuthor(term);
			}
			//按出版社查询
			if("1".equalsIgnoreCase(condionRad)){
				book.setPress(term);
			}
		}
		
		Paging paging=new Paging();
		paging.setCurrentPage(Integer.parseInt(currentPage));
		
		List<Book> bookList=bookService.queryBookList(book,paging);	
		
		/*
		 * 2.设置paging的url
		 */
		paging.setUrl(paging.acquireUrl(request));
		
		/*
		 * 3.页面需要的数据
		 */
		
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("bookList", bookList);
		data.put("paging", paging);
		
		/*
		 * 4.返回
		 */
		request.setAttribute("data",data);
		request.setAttribute("searchName",term);
		request.setAttribute("condionRad",condionRad);
		return "front/main";
	}
	
	/**
	 * @return
	 *  按照分类获取图书列表(分页)
	 */
	@RequestMapping(value = "list2", method = RequestMethod.GET)
	public String list2(HttpServletRequest request){
		/*
		 * 1.获取查询条件参数：查询条件、查询页码（如果页码参数为空，则设置为1）
		 */
		String currentPage=request.getParameter("currentPage");
		if(currentPage==null){
			currentPage="1";
		}
		Book book=new Book();
		//按书分类查询
		String cid=request.getParameter("cid");
		if(cid!=null&&!cid.trim().isEmpty()){
			book.setCid(Integer.parseInt(cid));
		}
		Paging paging=new Paging();
		paging.setCurrentPage(Integer.parseInt(currentPage));
		
		List<Book> bookList=bookService.queryBookList(book,paging);	
		
		/*
		 * 2.设置paging的url
		 */
		paging.setUrl(paging.acquireUrl(request));
		
		/*
		 * 3.页面需要的数据
		 */
		
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("bookList", bookList);
		data.put("paging", paging);
		
		/*
		 * 4.返回
		 */
		request.setAttribute("data",data);
		return "front/main";
	}
	
	/**
	 * @param request
	 * @return
	 *  查询主页默认的图书列表
	 */
	@RequestMapping("home")
	public String queryHomePageList(HttpServletRequest request){
		List<Book> homeList=bookService.queryHomePageList(1);
		request.setAttribute("homeList", homeList);
		return "front/home";
	}
	
	/**
	 * @param bid
	 * @param request
	 * @return
	 *  获取图书得详细信息
	 */
	@RequestMapping("detail")
	public String getDetail(@RequestParam String bid,HttpServletRequest request){
		Book book=bookService.queryByBid(Integer.parseInt(bid));
		request.setAttribute("book",book);
		return "front/detail";
	}
	
	/**
	 * 评价书籍
	 * @param bid
	 * @param request
	 * @return
	 */
	@RequestMapping("evaluation")
	public String evaluation(@RequestParam String bid, @RequestParam String oid,  @RequestParam String orderTime, HttpServletRequest request){
		Book book=bookService.queryByBid(Integer.parseInt(bid));
		request.setAttribute("book", book);
		request.setAttribute("oid", oid);
		request.setAttribute("orderTime", orderTime);
		return "front/evaluation";
	}
	
	@RequestMapping(value = "subEvaluation", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> subEvaluation(String oid, String score, String evaluation, String impression) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<String> list = JSON.parseArray(impression, String.class);
		String impressionStr = "";
		if (impression != null && list.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for (String impreStr : list) {
				sb.append(impreStr);
				sb.append(";");
			}
			impressionStr = sb.toString();
		}
		//构建实体
		OrderItemForm entity = new OrderItemForm();
		entity.setOid(oid);
		entity.setEvaluation(evaluation);
		entity.setScore(score);
		entity.setImpression(impressionStr);
		try {
			orderService.evaluationOrder(entity);
			orderService.setOrderStatus(oid, 5);
			result.put("errorCode", "0");
			result.put("msg", "评价成功");
		} catch (Exception e) {
			result.put("errorCode", "-1");
			result.put("msg", "评价失败，系统出现异常");
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("test")
	public String test(){
		return "front/test";
	}
}
